package com.tongji.seller.view.comp
{
	import alternativa.engine3d.loaders.collada.collada;
	
	import com.tongji.seller.events.AddRecentSceneEvent;
	import com.tongji.seller.events.AddStoreSceneEvent;
	import com.tongji.seller.events.DelStoreSceneEvent;
	import com.tongji.seller.events.GetStoreFavoriteStateEvent;
	import com.tongji.seller.events.GetStoreRecentOperateSceneStateEvent;
	import com.tongji.seller.events.GetStoreSavedSceneEvent;
	import com.tongji.seller.events.GetStoreSystemSceneHotStateEvent;
	import com.tongji.seller.events.GetStoreSystemSceneStateEvent;
	import com.tongji.seller.events.GetTopHotSceneEvent;
	import com.tongji.seller.events.utilsEvent.BubblesEvent;
	import com.tongji.seller.events.utilsEvent.PageChangeEvent;
	import com.tongji.seller.model.GetSystemSceneModel;
	import com.tongji.seller.model.SearchSceneModel;
	import com.tongji.seller.model.StoreSceneModel;
	import com.tongji.seller.model.UserModel;
	import com.tongji.seller.utils.PageTools;
	import com.tongji.seller.utils.SelectTools;
	import com.tongji.seller.utils.ShowCustomToolTip;
	
	import flash.events.MouseEvent;
	
	import mx.binding.utils.BindingUtils;
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.events.CollectionEvent;
	
	import org.osmf.events.BufferEvent;
	import org.robotlegs.mvcs.Mediator;
	
	public class SceneViewMediator extends Mediator
	{
		[Inject]
		public var view:SceneView;
		
		[Inject]
		public var sceneModel:GetSystemSceneModel;
		
		[Inject]
		public var storeSceneModel:StoreSceneModel;
		
		[Inject]
		public var searchSceneModel:SearchSceneModel;
		
		[Inject]
		public var userModel:UserModel;
		
		[Bindable]
		public var sceneCount:String;
		
		private var pageIndex:Number = 1;
		
		override public function onRegister():void
		{
			BindingUtils.bindProperty(view.sceneCount, "text", storeSceneModel, "savedSceneCount");
			view.selectedSceneComp.createDataProvider(storeSceneModel.selectedSceneArray);
			
			eventMap.mapListener(view.hotSceneBtn, MouseEvent.CLICK, hotSceneHandler);
			eventMap.mapListener(view.allSceneBtn, MouseEvent.CLICK, searchAllSceneHandler);
			eventMap.mapListener(view.collectSceneBtn, MouseEvent.CLICK, showStoreSceneHandler);
			eventMap.mapListener(view.recentSceneBtn, MouseEvent.CLICK, recentSceneHandler);
			
			addViewListener(PageChangeEvent.PAGE_INDEX_CHANGE, changePageHandler, PageChangeEvent);			
			addViewListener(BubblesEvent.SELECT_SCENE, selectSceneHandler, BubblesEvent);
			addViewListener(BubblesEvent.COLLECT_SCENE, collectSceneHandler, BubblesEvent);
			addViewListener(BubblesEvent.REMOVE_COLLECT_SCENE, removeCollectSceneHandler, BubblesEvent);
			
			addContextListener(GetStoreSavedSceneEvent.UPDATE_SAVED_SCENE, createDataHandler, GetStoreSavedSceneEvent);

			storeSceneModel.selectedSceneArray.addEventListener(CollectionEvent.COLLECTION_CHANGE, test);

			/*eventMap.mapListener(view.selectAllSceneBtn, MouseEvent.CLICK, selectAllSceneHandler);
			eventMap.mapListener(view.selectOtherSceneBtn, MouseEvent.CLICK, selectOtherSceneHandler);*/
			/*addContextListener(GetSystemSceneEvent.UPDATE_SYSTEM_SCENE, createDefaultPageHandler, GetSystemSceneEvent);*/
			//addContextListener(GetStoreSystemSceneStateEvent.UPDATE_STORE_SYSTEM_SCENE_STATE, createSearchPageHandler, GetStoreSystemSceneStateEvent);
			//addContextListener(GetStoreFavoriteStateEvent.UPDATE_STORE_FAVORITE_STATE, createFavoritePageHandler, GetStoreFavoriteStateEvent);
			//addContextListener(GetStoreRecentOperateSceneStateEvent.UPDATE_STORE_RECENT_OPERATE_SCENE_STATE, createRecentPageHandler, GetStoreRecentOperateSceneStateEvent);									
		}
		
		/**
		 *热门场景 
		 * @param event
		 * 
		 */		
		private function hotSceneHandler(event:MouseEvent):void
		{
			storeSceneModel.stateVar = 2;
			var hotEvent:GetStoreSystemSceneHotStateEvent = new GetStoreSystemSceneHotStateEvent(GetStoreSystemSceneHotStateEvent.GET_STORE_SYSTEM_SCENE_HOT_STATE, 
				                                                                                  userModel.currentUserID, "", "", storeSceneModel.hotScenePageIndex);
			dispatch(hotEvent);
			/*BindingUtils.bindProperty(view.sceneViewList, "dataProvider", storeSceneModel, "hotSceneArray");*/
			/*dispatch(new GetStoreHotSceneEvent(GetStoreHotSceneEvent.GET_STORE_HOT_SCENE, 10));
			BindingUtils.bindProperty(view.sceneViewList, "dataProvider", storeSceneModel, "hotSceneArray");*/
		}
		
		/**
		 * 获取所有场景 
		 * @param event
		 * 
		 */		
		private function searchAllSceneHandler(event:MouseEvent):void
		{
			storeSceneModel.stateVar = 0;
			dispatch( new GetStoreSystemSceneStateEvent(GetStoreSystemSceneStateEvent.GET_STORE_SYSTEM_SCENE_STATE,
				                                         userModel.currentUserID, searchSceneModel.cdIdStr, searchSceneModel.fgIdStr, storeSceneModel.searchScenePageIndex));
			
		}
		
		/**
		 *我的收藏 
		 * @param event
		 * 
		 */		
		private function showStoreSceneHandler(event:MouseEvent):void
		{
			storeSceneModel.stateVar = 3;
			
			dispatch(new GetStoreFavoriteStateEvent(GetStoreFavoriteStateEvent.GET_STORE_FAVORITE_STATE, userModel.currentUserID, storeSceneModel.favScenePageIndex));
			//BindingUtils.bindProperty(view.sceneViewList, "dataProvider", storeSceneModel, "favoriteSceneArray");
			//BindingUtils.bindProperty(view.sceneViewList, "dataProvider", storeSceneModel, "favoriteStateSceneArray");
		}
		
		/**
		 *最近使用 
		 * @param event
		 * 
		 */		
		private function recentSceneHandler(event:MouseEvent):void
		{
			storeSceneModel.stateVar = 4;
			
			dispatch(new GetStoreRecentOperateSceneStateEvent(GetStoreRecentOperateSceneStateEvent.GET_STORE_RECENT_OPERATE_SCENE_STATE, 
				                                              userModel.currentUserID, storeSceneModel.recentScenePageIndex, 18));
			BindingUtils.bindProperty(view.sceneViewList, "dataProvider", storeSceneModel, "recentSceneArray");
		}
		
		/**
		 * 数据分页 
		 * @param event
		 * 
		 */		
		private function changePageHandler(event:PageChangeEvent):void
		{
			pageIndex = event.pageIndex;
			switch(storeSceneModel.stateVar)
			{
				case 0:
					storeSceneModel.searchScenePageIndex = pageIndex;
					dispatch(new GetStoreSystemSceneStateEvent(GetStoreSystemSceneStateEvent.GET_STORE_SYSTEM_SCENE_STATE, userModel.currentUserID, searchSceneModel.cdIdStr, searchSceneModel.fgIdStr, pageIndex));					
					break;
				case 1:
					storeSceneModel.searchScenePageIndex = pageIndex;
					dispatch(new GetStoreSystemSceneStateEvent(GetStoreSystemSceneStateEvent.GET_STORE_SYSTEM_SCENE_STATE, userModel.currentUserID, searchSceneModel.cdIdStr, searchSceneModel.fgIdStr, pageIndex));
					break;
				case 2:
				{
					storeSceneModel.hotScenePageIndex = pageIndex;
					dispatch(new GetStoreSystemSceneHotStateEvent(GetStoreSystemSceneHotStateEvent.GET_STORE_SYSTEM_SCENE_HOT_STATE, userModel.currentUserID, "","",pageIndex));
					break;
				}					
				case 3:
				{
					storeSceneModel.favScenePageIndex = pageIndex;
					dispatch(new GetStoreFavoriteStateEvent(GetStoreFavoriteStateEvent.GET_STORE_FAVORITE_STATE, userModel.currentUserID, pageIndex));
					break;
				}
				case 4:
				{
					storeSceneModel.recentScenePageIndex = pageIndex;
					dispatch(new GetStoreRecentOperateSceneStateEvent(GetStoreRecentOperateSceneStateEvent.GET_STORE_RECENT_OPERATE_SCENE_STATE, userModel.currentUserID, pageIndex));
					break;
				}					
				default:
					break;
			}
		}
		
		
		/**
		 * 选择场景
		 * @param event
		 * 
		 */		
		private function selectSceneHandler(event:BubblesEvent):void
		{
			var sIdArray:Array = new Array();
			sIdArray.push(event.object.sid);
			dispatch(new AddStoreSceneEvent(AddStoreSceneEvent.ADD_STORE_USER_SYSTEM_SCENE, userModel.currentUserID, 0, sIdArray));
			dispatch(new AddRecentSceneEvent(AddRecentSceneEvent.ADD_RECENT_SCENE, userModel.currentUserID, event.object.sid));
		}
		
		
		/**
		 *收藏场景 
		 * @param event
		 * 
		 */		
		
		private function collectSceneHandler(event:BubblesEvent):void
		{
			dispatch(new AddStoreSceneEvent(AddStoreSceneEvent.ADD_STORE_FAVORITE_SCENE, userModel.currentUserID, event.object.sid));
		}
		
		/**
		 * 移除收藏 
		 * @param event
		 * 
		 */		
		private function removeCollectSceneHandler(event:BubblesEvent):void
		{
			dispatch(new DelStoreSceneEvent(DelStoreSceneEvent.DEL_STORE_FAVORITE, userModel.currentUserID, event.object.sid));
		}	
		
		/**
		 * 更新数据源 
		 * @param event
		 * 
		 */		
		private function createDataHandler(event:GetStoreSavedSceneEvent):void
		{
			view.selectedSceneComp.createDataProvider(storeSceneModel.selectedSceneArray);
		}
		
		protected function test(event:CollectionEvent):void
		{
			storeSceneModel.savedSceneCount = String(storeSceneModel.selectedSceneArray.length);
		}
	
		/*-------------------------------------------------------------------------------

		*全选场景 
		private function selectAllSceneHandler(event:MouseEvent):void
		{			
			var tempArray:ArrayCollection = view.sceneViewList.dataProvider as ArrayCollection;
			view.selectedSceneComp.createDataProvider(SelectTools.selectAll(sceneModel.selectSceneArray, tempArray));			
		}
		
		*反选场景 	
		private function selectOtherSceneHandler(event:MouseEvent):void
		{
			var tempArray:ArrayCollection = view.sceneViewList.dataProvider as ArrayCollection;
			//view.selectSceneComp.createDataProvider(SelectTools.selectOther(sceneModel.selectSceneArray, tempArray));
		}
		
		private function createDefaultPageHandler(event:GetStoreSystemSceneHotStateEvent):void
		{
			//view.sceneViewList.dataProvider = sceneModel.getSceneArr;
			BindingUtils.bindProperty(view.sceneViewList, "dataProvider", storeSceneModel, "sceneArray");
			var page:PageTools = new PageTools(storeSceneModel.recordSystemStateCount, 10, 6, 1);
			view.scenePageComp.createDataProvider([page.nums, page.pageArray, page.pageCount, pageIndex]);
		}
		
		private function createSearchPageHandler(event:GetStoreSystemSceneStateEvent):void
		{
			var page:PageTools = new PageTools(storeSceneModel.recordSystemStateCount, 10, 6, 1);
			view.scenePageComp.createDataProvider([page.nums, page.pageArray, page.pageCount, pageIndex]);
		}
		
		private function createFavoritePageHandler(event:GetStoreFavoriteStateEvent):void
		{
			var page:PageTools = new PageTools(storeSceneModel.recordFavoriteStateCount, 10, 6, 1);
			view.scenePageComp.createDataProvider([page.nums, page.pageArray, page.pageCount, pageIndex]);
		}
		
		private function createRecentPageHandler(event:GetStoreRecentOperateSceneStateEvent):void
		{
			var page:PageTools = new PageTools(storeSceneModel.recordRecentOperateSceneStateCount, 10, 6, 1);
			view.scenePageComp.createDataProvider([page.nums, page.pageArray, page.pageCount, pageIndex]);
		}
		------------------------------------------------------------------------------------*/
	}
}